// pages/payorder/payorder.js
const app = getApp();
var carid = '';
var goodid = '';
var goodnum = '';
var timegoodsid = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {

    shoperSend: 'view_titleSele',
    selfSend: 'view_defult',

    couponid: '', //优惠券id

    fareIsHiden: true, //是否显示配送方式
    shopList: [],
    payData:{},
    // 快递 1快递，2自提

    // 地址
    // address:{name:'用户名',phone:'15000000034','address':'天津市西青区杨柳青镇新华道220号正和宜公司'},
    address: '',
    // 快递
    peisong: '普通快递',
    // 优惠
    youhui: '选择优惠券',
    money: '0',
    yunfei: '0',
    sumbefo: '0', //原来总价            
    sum: '0',
    couponprice: 0, //优惠券优惠价格
    //自取


    //提交订单
    peisong_type: 1, //1是商家配送，2是到店自取


    selfName: '', //到店自取名称
    selfPhone: '', //到店自取手机号
    getDate: '请点击选择提货日期',
    getTime: '请选择提货时间',


    getName: '', //收获人名称
    getPhone: '', //收货人手机号
    getAddre: '', //收货人地址
    message: '', //留言

    huodong: true, //是否活动,
    level: 0, //等级
    levelName: '普通会员', //会员称号
    reduce: '95',
    sumold: '' //商品未合计的金额
  },

  //商品配送
  shoperSend: function() {
    this.setData({
      shoperSend: 'view_titleSele',
      selfSend: 'view_defult',
      peisong_type: 1,
      fareIsHiden: false
    })
  },
  //到店自取
  selfSend: function() {
    this.setData({
      shoperSend: 'view_defult',
      selfSend: 'view_titleSele',
      peisong_type: 2,
      fareIsHiden: true
    })
  },

  //选择地址
  addAddredd: function() {
    var self = this;
    wx.chooseAddress({
      success: function(res) {
        // console.log(res);
        self.setData({
          address: {
            name: res.userName,
            phone: res.telNumber,
            address: res.cityName + res.countyName + res.detailInfo
          },
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '请打开访问您地址的权限',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var self = this;
    options.goodsnumber=1;
    var shopData=[options];
    console.log(options);
    self.setData({
      payData:options,
      shopList: shopData,
      sumold:options.goodsmoney,
      sum: Number(options.goodsmoney).toFixed(2)
    })
    // self.shopList.goodsname = options.goodsname;
    // self.shopList.goodsnumber = 1;
    // self.shopList.goodsimg = options.goodsname;
    // self.shopList.goodsmoney = options.goodsmoney;
  },

  // 显示等级
  showlevel: function() {
    var self = this;
    switch (self.data.level) {

      // case 0:
      //   self.setData({
      //     dengjiIcon: '/image/chuji.png'
      //   })
      //   break;
      case 0:
        self.setData({
          levelName: '普通会员',
          huodong: true
        })
        break;
      case 1:
        self.setData({
          levelName: '高级会员',
          reduce: '95',
          huodong: false
        })
        break;
      case 2:
        self.setData({
          levelName: '白银会员',
          reduce: '85',
          huodong: false
        })
        break;
      case 3:
        self.setData({
          levelName: '黄金会员',
          reduce: '75',
          huodong: false
        })
        break;
      case 4:
        self.setData({
          levelName: '钻石会员',
          reduce: '65',
          huodong: false
        })
        break;
    }
  },

  //到店自取选择提货时间
  bindDateChange: function(e) {
    this.setData({
      getDate: e.detail.value
    })
  },


  bindTimeChange: function(e) {
    this.setData({
      getTime: e.detail.value
    })
  },

  //到店自取输入提货人
  meName(e) {
    this.setData({
      selfName: e.detail.value
    })
  },

  //到店自取输入手机号
  mePhone(e) {
    this.setData({
      selfPhone: e.detail.value
    })
  },
  //选择优惠券
  selectCoupon() {
    wx.navigateTo({
      url: '/pages/couponlist/couponlist?select=true',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // var price = this.data.sumbefo - this.data.couponprice;
    // this.setData({
    //   sum: price.toFixed(2)
    // })
  },

  //给商家留言
  leavMessage: function(e) {
    this.setData({
      message: e.detail.value,
    })
  },


  //提交订单
  order_submit: function() {
    var self = this;
    var url = app.serverUrl + 'About/orderAdd';
    var name = '';
    var tel = '';
    var orderaddress = '';

    if (self.data.peisong_type == '1') {
      //商家配送
      if (self.data.address == '' || self.data.address == null) {
        wx.showToast({
          title: '请选择地址',
        })
        return;
      }
      name = self.data.address.name;
      tel = self.data.address.phone;
      //如果收获地址为空
      orderaddress = self.data.address.address;

    } else {
      //到店自取
      name = self.data.selfName;
      tel = self.data.selfPhone;
      receivedtime = self.data.getDate;

      var title = '';
      if (name == '') {
        title = '请填写姓名'
        wx.showToast({
          title: title,
          icon: 'none'
        })
        return;
      }
      if (tel == '') {
        title = '请填写联系方式'
        wx.showToast({
          title: title,
          icon: 'none'
        })
        return;
      }
      if (self.data.getDate == '请点击选择提货日期') {
        title = '请点击选择提货日期'
        wx.showToast({
          title: title,
          icon: 'none'
        })
        return;
      }
      if (self.data.getTime == '请选择提货时间') {
        title = '请选择提货时间'
        wx.showToast({
          title: title,
          icon: 'none'
        })
        return;
      }
    }
    var receivedtime = '' + self.data.getDate + ' ' + self.data.getTime;
    // console.log(app.openid)
    // console.log(name)
    // console.log(tel)
    // console.log(self.data.peisong_type)
    // console.log(carid.toString())
    // console.log(goodid)
    // console.log(goodnum)
    // console.log(orderaddress)
    // console.log(receivedtime)
    // console.log(self.data.message)
    // console.log(self.data.couponid)
    // timegoodsid = options.timegoodsid;
    var orderid = '';
    wx.request({
      url: url,
      method: 'post',
      data: {
        openid: app.openid,
        username: name,
        tel: tel,
        ordertype: self.data.peisong_type,
        goodsid: self.data.payData.goodsid,
        goodsnumber: self.data.payData.goodsnumber,
        orderaddress: orderaddress,
        liuyan: self.data.message,
      },
      success: function(res) {
        console.log(res);
        if(res.data.flag=='error'){
          wx.showToast({
            image: '/image/icon_ca.png',
            title: res.data.message,
          })
          return
        }
        orderid = res.data.orderid;
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          complete: function(res) {
            console.log(res)
            if (res.errMsg == 'requestPayment:fail cancel' || res.errMsg == 'requestPayment:ok') {
              wx.redirectTo({
                url: '/pages/orderdetail/orderdetail?orId=' + orderid,
              })
            } else {
              wx.showToast({
                image: '/image/icon_ca.png',
                title: '支付失败',
              })
            } 
          }
        })





      },
      fail: function() {
        wx.showModal({
          title: '提交失败，请填写完信息重新提交！',
          content: '',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  }
})